<template>
  <div class="box">
    <el-table :data="tableData" stripe>
      <el-table-column prop="id" label="用户ID"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名"> </el-table-column>
      <el-table-column prop="price" label="单价">
        <template slot-scope="scope">￥{{ scope.row.price }} </template>
      </el-table-column>
      <el-table-column prop="number" label="数量"> </el-table-column>
      <el-table-column prop="order_type" label="商品类型"> </el-table-column>
      <el-table-column prop="images" label="商品图片">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="'http://localhost:8081/' + scope.row.images"
            alt=""
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" class="cz-btn">
        <template slot-scope="scope">
          <button @click="storexg(scope.row)">修改</button>/<button
            @click="storesc(scope.row)"
          >
            删除
          </button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      background
      :page-sizes="[2, 3, 5, 10]"
      :page-size="queryinfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 修改商品 -->
    <el-dialog title="修改商品" :visible.sync="storexgb">
      <el-form :model="forms">
        <el-form-item label="商品名">
          <el-input v-model="forms.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单价">
          <el-input v-model="forms.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="forms.number" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adminxgqx = false">取 消</el-button>
        <el-button type="primary" @click="xgadds()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加商品-->
    <el-button type="text" @click="dialogFormVisible = true"
      >添加新的商品</el-button
    >
    <el-dialog title="添加新商品" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="form.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品单价" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" :label-width="formLabelWidth">
          <el-input v-model="form.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上传商品图片" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            action="http://localhost:8081/admin/storeimage"
            :on-success="handleSuccess"
            :on-preview="handlePreview"
            name="logo"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品类型" :label-width="formLabelWidth">
          <el-select v-model="form.order_type" placeholder="请选择商品类型">
            <el-option
              v-for="item in order_type1"
              :key="item.id"
              :label="item.order_type"
              :value="item.order_id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="storeadd"> 确 定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

 <script>
import axios from "axios";
export default {
  data() {
    return {
      queryinfo: {
        //第几页
        page: 1,
        //显示的页数
        pageSize: 2,
      },
      total: 0,

      forms: {
        // username: "",
        // password: "",
        // email: "",
        // nickname: "",
      },
      adminxgqx: false,
      storexgb: false,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        order_type: [],
        image: "",
      },
      order_type1: [],
      formLabelWidth: "120px",
      tableData: [],

      // 图片上传组件的headers请求头对象
      headerObj: {},
      fileList: [],
    };
  },
  methods: {
    // async 异步函数
    async getStore() {
      // 使用try catch捕获错误
      try {
        // 使用axios发送ajax请求，获取用户请求
        // 把请求的结果拿到给一个变量，await是搭配异步函数等待执行完才会执行下面的代码
        let res = await this.$http.get("admin/getStore", {
          params: this.queryinfo,
        });
        // 打印返回的结果
        console.log(res.data.data.data);
        // 对象解构，拿到数据，相当于code = res.data.code msg = res.data.msg data = res.data.data;
        let { code, msg, data } = res.data;
        // 如果返回的码不是200就代表出错了，调用ElementUI显示出错内容，return阻止代码向下执行
        if (code !== 200) return this.$message.error(msg);
        // 否则代表成功，打印看下成功数据
        // console.log(data);
        // 替换表格数据
        this.total = res.data.data.total;
        this.tableData = res.data.data.data;
      } catch (err) {
        // 如果发送就出错了就会在这里处理错误事件
        // 调用ElementUI 给一个错误提示
        this.$message.error(err.message);
      }
    },
    getType() {
      axios.get("http://localhost:8081/admin/getType").then((res) => {
        this.order_type1 = res.data.data;
        console.log(this.order_type1);
      });
    },
    // 监听pagesize改变时间
    handleSizeChange(val) {
      // console.log(newsize);
      this.queryinfo.pageSize = val;
      this.getStore();
    },

    //  监听当前页面改变事件
    handleCurrentChange(val) {
      this.queryinfo.page = val;
      this.getStore();
    },
    storeadd() {
      axios
        .post("http://localhost:8081/admin/storeadd", this.form)
        .then((res) => {
          console.log(res);
          this.dialogFormVisible = false;
          this.getStore();
        });
    },
    storexg(row) {
      this.storexgb = true;
      this.forms = row;
      this.forms.id = row.id;
      console.log(this.forms);
    },
    storesc(row) {
      this.forms.id = row.id;
      console.log(this.forms.id);
      axios
        .post("http://localhost:8081/admin/storesc", this.forms)
        .then((res) => {
          this.getStore();
        });
    },
    handlePreview() {},
    handleSuccess(res) {
      this.form.image = res.data;
    },
    xgadds() {
      // console.log("sss");
      axios
        .post("http://localhost:8081/admin/xgadds", this.forms)
        .then((res) => {
          // console.log(res);
          this.storexgb = false;
          this.getStore();
        });
    },
  },

  // 页面被挂载时候触发
  mounted() {
    this.getStore();
    this.getType();
  },
};
</script>
<style >
.box {
  width: 100%;
}
</style>